package tools;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.exception.HttpProcessException;

public class HotSearch {
	private static String url = "https://weibo.com/ajax/side/hotSearch";
	private static String aimUrl = "https://s.weibo.com/weibo?q=%%23%s%%23";
	private static String wburl = "https://weibo.com/ajax/statuses/hot_band";
	private static String picurl = "https://wx4.sinaimg.cn/orj360/%s.jpg";
	
	public static void main(String[] args) {
		String jsonStr = test2();
		System.out.println(jsonStr);
		List<Hot> hotList = getHotFromJson(jsonStr);
		System.out.println("解析结果");
		for (Hot hot : hotList) {
			System.out.println(hot.getJsonObject());
		}
	}
	//json字符串转Hot的List对象
	public static List<Hot> getHotFromJson(String jsonStr) {
		List<Hot> hotList = new ArrayList<>();
		JSONArray jsonArray = new JSONArray(jsonStr);
		for(int i=0;i<jsonArray.length();i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			Hot hot = new Hot(jsonObject);
			hotList.add(hot);
		}
		return hotList;
	}
	//获取带内容的热搜,返回json数组字符串
	public static String test2() {
		HttpConfig config = HttpConfig.custom().url(wburl).encoding("utf-8");
		JSONArray resultsJsonArray = new JSONArray();
		try {
			String result = HttpClientUtil.get(config);
			System.out.println(result);
			JSONObject obj = new JSONObject(result);
			obj = obj.getJSONObject("data");
			JSONArray jsArray = obj.getJSONArray("band_list");
			System.out.println(jsArray.length());
			for(int i=0;i<jsArray.length();i++) {
				obj = jsArray.getJSONObject(i);
				//非商业投放
	            if(obj.isNull("is_ad")) {
	            	String mid = "", word = "", category = "", label_name = "", text = "", pic = "";
	                long onboard_time = 0L, num = 0L;
	                int rank = -1;
	                try {
	                    rank = obj.getInt("rank") + 1;
	                    try {
                            mid = obj.getString("mid");
                        } catch (Exception e) {
                        }

                        try {
                            word = obj.getString("word");
                        } catch (Exception e) {
                        }

                        try {
                            category = obj.getString("category");
                        } catch (Exception e) {
                        }

                        try {
                            label_name = obj.getString("label_name");
                        } catch (Exception e) {
                        }

                        try {
                            onboard_time = obj.getLong("onboard_time");
                        } catch (Exception e) {
                        }

                        try {
                            num = obj.getLong("num");
                        } catch (Exception e) {
                        }
                        
                        try {
                            JSONObject mblog = obj.getJSONObject("mblog");
                            text = mblog.getString("text");
                            int pic_num = mblog.getInt("pic_num");
                            if(pic_num>0) {
                            	JSONArray picArray = mblog.getJSONArray("pic_ids");
                            	pic = String.format(picurl, picArray.getString(0));//取第一个封面
                            }else {
                            	JSONObject page_info = mblog.getJSONObject("page_info");
                            	pic = page_info.getString("page_pic");
                            }
                        } catch (Exception e) {
                        }
                        
                        Hot hot = new Hot(mid, word, onboard_time, category, num, rank, label_name, String.format(aimUrl, word));
                        hot.setText(text);
                        hot.setPic(pic);
                        resultsJsonArray.put(hot.getJsonObject());
	                } catch (Exception e) {
                    }
	            }
			}
		} catch (HttpProcessException e) {
			e.printStackTrace();
		}
		return resultsJsonArray.toString();
	}
	
	//获取实时热搜榜
	public static void test1() {
		HttpConfig config = HttpConfig.custom().url(url).encoding("utf-8");
		try {
			String result = HttpClientUtil.get(config);
			//请求接口的时间,13位时间戳
			long getTime = Calendar.getInstance().getTimeInMillis();
			System.out.println(result);
			JSONObject obj = new JSONObject(result);
			obj = obj.getJSONObject("data");
			JSONArray jsArray = obj.getJSONArray("realtime");
			for(int i=0;i<jsArray.length();i++) {
				obj = jsArray.getJSONObject(i);
	            //非商业投放
	            if(obj.isNull("is_ad")) {
	                String mid = "", word = "", category = "", label_name = "";
	                long onboard_time = 0L, num = 0L;
	                int rank = -1;

	                try {
	                    rank = obj.getInt("rank") + 1;
	                    if (rank <= 10) {
	                        try {
	                            mid = obj.getString("mid");
	                        } catch (Exception e) {
	                        }

	                        try {
	                            word = obj.getString("word");
	                        } catch (Exception e) {
	                        }

	                        try {
	                            category = obj.getString("category");
	                        } catch (Exception e) {
	                        }

	                        try {
	                            label_name = obj.getString("label_name");
	                        } catch (Exception e) {
	                        }

	                        try {
	                            onboard_time = obj.getLong("onboard_time");
	                        } catch (Exception e) {
	                        }

	                        try {
	                            num = obj.getLong("num");
	                        } catch (Exception e) {
	                        }

	                        Hot hot = new Hot(mid, word, onboard_time, category, num, rank, label_name, String.format(aimUrl, word));
	                        System.out.println(hot.toString());
	                    }

	                } catch (Exception e) {
	                }
	            }
			}
		} catch (HttpProcessException e) {
			e.printStackTrace();
		}
	}

}
